<template>
  <div>
    <el-card>
      <!-- 头部 -->
      <div slot="header">
        <span>添加账号</span>
        <el-button
          style="float: right; padding: 3px 0"
          type="text"
        >操作按钮</el-button>
      </div>
      <!-- 内容 -->
      <div class="content">
        <el-form
          status-icon
          size="small"
          ref="addAccountFrom"
          :model="addAccountForm"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item
            label="账号"
            prop="account"
          >
            <el-input
              v-model="addAccountForm.account"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item
            label="密码"
            prop="password"
          >
            <el-input
              type="password"
              v-model="addAccountForm.password"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item
            label="用户组"
            prop="userGroup"
          >
            <el-select
              v-model="addAccountForm.userGroup"
              placeholder="请选择管理员身份"
            >
              <el-option
                label="超级管理员"
                value="超级管理员"
              > </el-option>
              <el-option
                label="普通管理员"
                value="普通管理员"
              ></el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              @click="submitForm"
            >添加</el-button>
            <el-button @click="resetForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { checkAccount, checkPassword } from "@/utils/tools";
import { addAccount } from "@/api/user";
export default {
  data() {
    return {
      addAccountForm: {
        account: "",
        password: "",
        userGroup: "",
      },
      rules: {
        account: [{ validator: checkAccount, trigger: "blur" }],
        password: [{ validator: checkPassword, trigger: "blur" }],
        userGroup: [
          { required: true, message: "请选择管理员身份", trigger: "change" },
        ],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs.addAccountFrom.validate(async (valid) => {
        if (valid) {
          let res = await addAccount(this.addAccountForm);
          let { code } = res.data;
          if (code === 0) {
            this.$router.push("/account/account-list");
          }
        } else {
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.addAccountFrom.resetFields();
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-input__inner {
  width: 230px;
}
</style>
